package com.example.springbootshiro02.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShrioConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean (@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        Map<String,String> map = new LinkedHashMap<>();
        /*
        * anon ：无需认证直接登录
        * authc:必须认证才能访问
        * user:必须拥有记住我才能访问
        * perms:拥有对某个资源的权限才能访问
        * role:用户某个角色才能访问
        * */
        //拦截请求
        map.put("/user/*","authc");
        bean.setFilterChainDefinitionMap(map);

        //设置登录请求
        bean.setLoginUrl("/tologin");
        return bean;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建对象realm 需要自定义类
    @Bean
    public UserRealm userRealm(){
        return  new UserRealm();
    }
}
